﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using You2V.LocalDBLayer;
using You2V.Data.Shared;

namespace You2VTool.Controls
{
    /// <summary>
    /// Interaction logic for Student2012Input.xaml
    /// </summary>
    public partial class Student2012InputControl : UserControl
    {
        public Student2012InputControl()
        {
            InitializeComponent();

            this.DataContext = this;

            this.AreaCombobox.ItemsSource = You2VDataDictionary.AreaList;
            this.GenderCombobox.ItemsSource = You2VDataDictionary.GenderList;
            this.NationCombobox.ItemsSource = You2VDataDictionary.NationList;


            SchoolList = new List<SchoolInfo>();

        }

        public List<SchoolInfo> SchoolList { get; set; }

        public StudentInfo2012 StudentBakForDirtyCheck { get; set; }
        public StudentInfo2012 CurrentStudent { get; set; }
        public bool IsEditing { get; set; }

        public void NewStudent()
        {
            CurrentStudent = new StudentInfo2012();
            StudentBakForDirtyCheck = (StudentInfo2012)CurrentStudent.Clone();

            this.DataContext = CurrentStudent;

            OprationStatus.Content = "新建学生";
            IsEditing = false;
            this.IsEnabled = true;
            this.TextBoxStudentNumber.IsReadOnly = false;
            StartRendering();
        }

        public void NewStudentFromExcel(StudentInfo2012 student)
        {
            CurrentStudent = student;
            StudentBakForDirtyCheck = (StudentInfo2012)CurrentStudent.Clone();

            this.DataContext = CurrentStudent;

            OprationStatus.Content = "新建学生(来自Excel)";
            IsEditing = false;
            this.IsEnabled = true;
            this.TextBoxStudentNumber.IsReadOnly = false;
            StartRendering();
        }

        public void Empty()
        {
            CurrentStudent = null;
            this.DataContext = CurrentStudent;

            IsEditing = false;
            OprationStatus.Content = "";
            this.IsEnabled = false; //todo 清空内容

        }

        public bool IsDataDirty()
        {
            if (!CurrentStudent.Equals(StudentBakForDirtyCheck))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void LoadStudent(StudentInfo2012 SelectedStudent, bool EnableEdit)
        {
            if (SelectedStudent == null)
            {
                MessageBox.Show("无法载入学生数据，错误编号86401259 请与技术组联系", "数据库错误");
                return;
            }

            CurrentStudent = (StudentInfo2012)SelectedStudent.Clone();
            StudentBakForDirtyCheck = (StudentInfo2012)CurrentStudent.Clone();

            this.DataContext = CurrentStudent;

            if (EnableEdit)
            {
                this.IsEnabled = true;

                IsEditing = true;

                //StudentNumber不允许修改？
                //this.TextBoxStudentNumber.IsReadOnly = true;
                this.TextBoxStudentNumber.IsReadOnly = false;

                OprationStatus.Content = "编辑学生:\t" + CurrentStudent.StudentNumber + " " + CurrentStudent.FullName;

            }
            else
            {
                this.IsEnabled = true;  //todo, 如果false，滚动条不出现，无法看到全部信息，所以暂时Enable, 
                //以后把每个control全部变成readonly
                IsEditing = false;

                OprationStatus.Content = "查看学生:\t" + CurrentStudent.StudentNumber + " " + CurrentStudent.FullName + "\t\t(注意:该页面无法修改学生信息，只能查看)";

            }

            StartRendering();
        }

        private void StartRendering()
        {
            //地区设置
            //if (CurrentStudent.StudentArea != null)
            //{
            //    for (int i = 0; i < You2VDataDictionary.AreaList.Count; i++)
            //    {
            //        if (You2VDataDictionary.AreaList[i].area == CurrentStudent.StudentArea.area)
            //        {
            //            this.AreaCombobox.SelectedIndex = i;
            //            break;
            //        }
            //    }
            //}

            //设置民族
            bool MatchNation = false;
            for (int i = 0; i < You2VDataDictionary.NationList.Count; i++)
            {
                if (You2VDataDictionary.NationList[i].Equals(CurrentStudent.Nation))
                {
                    this.NationCombobox.SelectedIndex = i;
                    MatchNation = true;
                    break;
                }
            }

            if (!MatchNation)
                this.NationCombobox.SelectedIndex = -1;

            //设置学校
            bool MatchSchool = false;
            for (int i = 0; i < SchoolList.Count; i++)
            {
                /*
                if (SchoolList[i].SchoolShortName.Equals(CurrentStudent.SchoolInfo.SchoolShortName)
                    && SchoolList[i].SchoolFullName.Equals(CurrentStudent.SchoolInfo.SchoolFullName))
                */

                //只比较fullName, 从Excel录入的数据只有FullName
                if (SchoolList[i].SchoolFullName.Equals(CurrentStudent.SchoolInfo.SchoolFullName))
                {
                    this.SchoolCombobox.SelectedIndex = i;
                    MatchSchool = true;
                    break;
                }
            }

            if (!MatchSchool)
                this.SchoolCombobox.SelectedIndex = -1;
        }

        public bool GetDataFromUI()
        {
            //请尽量按照界面顺序来进行提示
            string ErrorMessage = string.Empty;

            // 取得地区编号
            if (CurrentStudent.StudentArea == null)
            {
                ErrorMessage += "请选择学生所在地区\r\n";
            }

            //编号，姓名，别名不能为空
            if (string.IsNullOrWhiteSpace(CurrentStudent.StudentNumber))
            {
                ErrorMessage += "请输入学生编号\r\n";
            }

            if (string.IsNullOrWhiteSpace(CurrentStudent.FullName))
            {
                ErrorMessage += "请输入学生姓名\r\n";
            }

            if (string.IsNullOrWhiteSpace(CurrentStudent.NickName))
            {
                ErrorMessage += "请输入学生别名\r\n";
            }

            if (string.IsNullOrWhiteSpace(CurrentStudent.Gender))
            {
                ErrorMessage += "请输入学生性别\r\n";
            }
            
            if (string.IsNullOrWhiteSpace(CurrentStudent.Nation))
            {
                ErrorMessage += "请输入民族, 如果不清楚学生的民族，请选择‘未知’\r\n";
            }

            //学校不能为空
            if (SchoolCombobox.SelectedIndex < 0)
            {
                ErrorMessage += "请选择学校\r\n";
            }

            if (string.IsNullOrEmpty(ErrorMessage))
            {
                return true;
            }
            else
            {
                MessageBox.Show(ErrorMessage, "信息缺失");
                return false;
            }
        }

        private void AreaCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CurrentStudent == null)
                return;

            // 根据地区，改变学校列表
            if (CurrentStudent.StudentArea != null)
            {
                StudentDBL StudentDBL = new StudentDBL();
                SchoolList = StudentDBL.GetSchoolListByArea(CurrentStudent.StudentArea.area);
            }
            else
            {
                SchoolList.Clear();
            }

            this.SchoolCombobox.ItemsSource = SchoolList;

            //切换以后原来的SchoolInfo可能丢失，需要重新生成
            if (CurrentStudent.SchoolInfo == null)
                CurrentStudent.SchoolInfo = new SchoolInfo();

            for (int i = 0; i < SchoolList.Count; i++)
            {
                if (SchoolList[i].SchoolShortName.Equals(CurrentStudent.SchoolInfo.SchoolShortName)
                    && SchoolList[i].SchoolFullName.Equals(CurrentStudent.SchoolInfo.SchoolFullName))
                {
                    this.SchoolCombobox.SelectedIndex = i;
                }

            }
        }
    }
}
